﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NativeExcel;

namespace TPAMain工序结余锁定工具
{
    public partial class frmReport : Form
    {
        SqlTools sqt;

        string strDefaultString = "select a.ID 锁定序号,a.LockMode 锁定模式,a.LockKey 锁定关键词,LockReason 锁定原因,gxjy004 型号规格,gxjy007 工序,OriginalGxjy 原始工序结余,gxjy008 当前工序结余,OriginalBz 原始备注,SN 批号,ChipSN 芯片号,LockOperator 锁定人,LockOperateTime 锁定时间,[Unlock] 解锁,UnlockOperator 解锁人,UnlockTime 解锁时间,UnlockReason 解锁原因,GDNo 工单编号  from DefualtDataBase .dbo . LockMainLog a,DefualtDataBase .dbo .LockDetailLog b,TPAMain.dbo.gxjy_table where a.ID=b.MainID and gxjy000=b.GxjyID ";

        public frmReport()
        {
            InitializeComponent();
        }

        private void frmReport_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
            iniComboBox();
        }

        private void iniComboBox()
        {
            cmbGx.Items.Clear();
            string strTmpSelect = "select distinct gxjy007 from DefualtDataBase .dbo .LockDetailLog ,gxjy_table where GxjyID =gxjy000 order by gxjy007";
            sqt.FillComboBox(cmbGx, strTmpSelect);
            cmbGx.Items.Insert(0, "全部");
            cmbGx.SelectedIndex = 0;
            cmbStatus.SelectedIndex = 0;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            iniComboBox();
            txtGDNo.Clear();
            txtChipSN.Clear();
            txtSN.Clear();
            txtModel.Clear();
            chkLockTime.Checked = false;
            chkUnlockTime.Checked = false;

        }

        private void chkLockTime_CheckedChanged(object sender, EventArgs e)
        {
            dtpLockFrom.Enabled = chkLockTime.Checked;
            dtpLockTo.Enabled  = chkLockTime.Checked;
        }

        private void chkUnlockTime_CheckedChanged(object sender, EventArgs e)
        {
            dtpUnlockFrom.Enabled = chkUnlockTime.Checked;
            dtpUnlockTo.Enabled = chkUnlockTime.Checked;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strTmp = strDefaultString;
            if (!txtGDNo .Text .Trim ().Equals (string.Empty ))
            {
                strTmp += " and gxjy001 like '" + txtGDNo.Text.Trim() + "'";
            }
            if (!txtChipSN .Text .Trim ().Equals (string.Empty ))
            {
                strTmp += " and ChipSN like '" + txtChipSN.Text.Trim() + "'";
            }
            if (!txtSN.Text.Trim().Equals(string.Empty))
            {
                strTmp += " and SN like '" + txtSN.Text.Trim() + "'";
            }
            if (!txtModel .Text .Trim ().Equals (string.Empty ))
            {
                strTmp += " and gxjy004 like '" + txtModel.Text.Trim() + "'";
            }
            if (!cmbGx .Text .Equals ("全部"))
            {
                strTmp += " and gxjy007='" + cmbGx.Text + "'";
            }
            if (cmbStatus .Text .Equals ("未解锁"))
            {
                strTmp += " and [Unlock]=0";
            }
            else if (cmbStatus.Text.Equals("已解锁"))
            {
                strTmp += " and [Unlock]=1";
            }
            if (chkLockTime .Checked )
            {
                strTmp += " and LockOperateTime between '" + dtpLockFrom.Value.ToString("yyyy-MM-dd") + "' and '" + dtpLockTo.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (chkUnlockTime.Checked)
            {
                strTmp += " and UnlockTime between '" + dtpUnlockFrom.Value.ToString("yyyy-MM-dd") + "' and '" + dtpUnlockTo.Value.ToString("yyyy-MM-dd") + "'";
            }
            dgv1.DataSource = sqt.GetData(strTmp);
            dgv1.AutoResizeColumns();
        }

        private void frmReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void WriteDataToFile(string strFileName, DataTable dt)
        {
            IWorkbook tmpBook = Factory.CreateWorkbook();
            tmpBook.Worksheets.Add();
            tmpBook.Worksheets[1].Range.SetDataTable(dt, false);
            tmpBook.SaveAs(strFileName);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "*.xls|*.xls";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string strTmpFileName = sfd.FileName;
                    DataTable tmpdt = (DataTable)dgv1.DataSource;
                    WriteDataToFile(strTmpFileName, tmpdt);
                }
            }
        }
    }
}
